package com.wjjumpad.cwjjumpad.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.wjjumpad.cwjjumpad.R;
import com.wjjumpad.cwjjumpad.model.AppInfoModel;

import java.util.List;


public class AppInfoAdapter extends RecyclerView.Adapter<AppInfoAdapter.ViewHolder> {
    private List<AppInfoModel> nappInfoModelList;
    public AppInfoAdapter(List<AppInfoModel> appInfoModelList) {
        Log.d("AppInfoAdapter", "List: "+appInfoModelList);
        nappInfoModelList= appInfoModelList;
    }

    //静态内部类， 每个条目对应的布局
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView appName;

        ImageView appIcon;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            appName = (TextView) itemView.findViewById(R.id.item_text);
            appIcon = (ImageView) itemView.findViewById(R.id.item_icon);
        }
    }

    @NonNull
    @Override
    public AppInfoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        return null;
//        Log.d("AppInfoAdapter", "onCreateViewHolder - viewType: "+viewType);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_installed_app_list_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull AppInfoAdapter.ViewHolder holder, int position) {
        AppInfoModel appinfo = nappInfoModelList.get(position);
        Log.d("AppInfoAdapter", "onBindViewHolder: "+appinfo);
        if (position == 1){
            Log.d("AppInfoAdapter", "onBindViewHolder appinfo: "+appinfo);
        }
        holder.appName.setText(appinfo.getAppName());
        holder.appIcon.setImageDrawable(appinfo.getAppImg());
    }

    @Override
    public int getItemCount() {
        return nappInfoModelList.size();
    }


//    private int resourceId;
//    private List<AppInfoModel> objects;
//    private Context context;
//
//    public AppInfoAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<AppInfoModel> objects) {
//        super(context, resource, objects);
//    }
//
//    @Override
//    public int getCount() {
//        return objects.size();
//    }
//
//    @Nullable
//    @Override
//    public AppInfoModel getItem(int position) {
//        return objects.get(position);
//    }
//
//    @Override
//    public long getItemId(int position) {
//        return position;
//    }
//
//    @NonNull
//    @Override
//    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//        ViewHolder viewHolder = null;
//        if (convertView==null){
//            viewHolder = new ViewHolder();
//            LayoutInflater layoutInflater = LayoutInflater.from(context);
////            convertView = layoutInflater.inflate(R.layout.activity_list_item,null);
////            viewHolder.imageView = convertView.findViewById(R.id.iv_image);
////            viewHolder.title = convertView.findViewById(R.id.tv_title);
////            viewHolder.content = convertView.findViewById(R.id.tv_content);
//        }else {
//            viewHolder = (ViewHolder) convertView.getTag();
//        }
//        AppInfoModel appinfo = objects.get(position);
//        if (appinfo!=null){
////            viewHolder.imageView.setBackgroundResource(appinfo.getImg());
//            viewHolder.title.setText(appinfo.getTitle());
//            viewHolder.content.setText(appinfo.getContent());
//        }
//        return convertView;
//    }
//
//    private static class ViewHolder{
//        ImageView imageView;
//        TextView title;
//        TextView content;
//    }



}

